package com.boc.image.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.boc.bus.control.BusOutputResult;
import com.boc.ibatis.config.IbatisSqlClient;

public class UserMessage {
	/**
	 * 根据条件取得分页总记录数
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult getSumPages(HttpServletRequest request,HttpServletResponse response,Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			int totalPages = 0;//总页数
			int pageSize = Integer.parseInt(m.get("pageSize").toString());//每页显示多少行
			int totalRows = 0;//总行数
			m.put("nReceiveid", request.getSession().getAttribute("userId"));
			Integer count = (Integer)IbatisSqlClient.sqlMapClient.queryForObject("image_t_img_message.getRows",m);
			totalRows = count.intValue();
			totalPages = totalRows % pageSize == 0? (totalRows / pageSize):(totalRows / pageSize)+1;
			result.addValue("totalRows", totalRows);
			result.addValue("totalPages", totalPages);
			result.addValue("PN_RET", "0");
		} catch (Exception e) {
			result.addValue("PN_RET", "-1");
			result.addValue("PC_MSG", e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 取得当前页结果集
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult getPageRows(HttpServletRequest request,HttpServletResponse response,Map m) {
		BusOutputResult result = new BusOutputResult();
		int currPage = 0;
		int pageRows = 0;
		try {
			m.put("nReceiveid", request.getSession().getAttribute("userId"));
			currPage = Integer.parseInt(m.get("currPage").toString());
			pageRows = Integer.parseInt(m.get("pageRows").toString());
			List list = IbatisSqlClient.sqlMapClient.queryForList(
					"image_t_img_message.getPageRows", m,
					(currPage - 1) * pageRows, pageRows);
			result.addValues("P_RESULT", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * 取消息详细信息
	 * @param nId
	 * @param cYdzt
	 * @return
	 */
	public Map getMessageDetail(String nId,String cYdzt) {
		Map result = new HashMap();
		try{
			//是否需要设置阅读状态
			if (cYdzt.equals("0")) {
				IbatisSqlClient.sqlMapClient.update("image_t_img_message.updateYdzt",Integer.parseInt(nId));
			}
			List list = IbatisSqlClient.sqlMapClient.queryForList("image_t_img_message.getMessageDetail",Integer.parseInt(nId));
			if(list.size() == 1) {
				result = (Map)(list.get(0));
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 用户删除消息
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult deleteUserMessage(HttpServletRequest request,HttpServletResponse response,Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			IbatisSqlClient.sqlMapClient.delete("image_t_img_message.ibatorgenerated_deleteByPrimaryKey", Integer.parseInt(m.get("nId").toString()));
			result.addValue("PN_RET", "0");
			result.addValue("PC_MSG", "");
		} catch (Exception e) {
			result.addValue("PN_RET", "-1");
			result.addValue("PC_MSG", "系统异常");
			e.printStackTrace();
		}
		return result;
	} 
	
	/**
	 * 用户写消息
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult writeMessage(HttpServletRequest request,HttpServletResponse response,Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			String type = request.getParameter("cType");
			m.put("nSendid", request.getSession().getAttribute("userId"));
			if(type.equals("1")) {//新消息
				IbatisSqlClient.sqlMapClient.insert("image_t_img_message.writeNewMessage",m);
			} else {//回复消息新消息
				IbatisSqlClient.sqlMapClient.insert("image_t_img_message.recevieNewMessage",m);
				/*
				 * 更新原来的消息,设置状态为已回复
				 */
				IbatisSqlClient.sqlMapClient.update("image_t_img_message.setMsgZt",m);
			}
			result.addValue("PN_RET", "0");
			result.addValue("PC_MSG", "");
		} catch (Exception e) {
			result.addValue("PN_RET", "-1");
			result.addValue("PC_MSG", "系统异常");
			e.printStackTrace();
		}
		return result;
	}
}
